<?php
Globals::requireClass('Controller');
Globals::requireModel('User');
Globals::requireTable('Serve');

class ServeController extends Controller
{
    protected $ServeTable;
	
	public static $defaultConfig = array(
		'viewEnabled'	=> true,
		'layoutEnabled'	=> true,
		'title'			=> null
	);
	
	public function __construct($config = null)
	{
	    parent::__construct($config);
	    $this->ServeTable = new ServeTable($config);
		dolog(json_encode($_GET));
	}
	
	public function indexAction()
	{
		$this->layout->pageTitle	= "test";
	}
	
	
	public function serveAction(){
	    $uid = $this->getParam('uid');
	    $token = $this->getParam('token');
	    $machine_type = $this->getParam('machine_type');
	    $usermodel = new UserModel($this->config);
	    $uid = $usermodel->getUserBase($uid, $token ,$machine_type);
	    if($uid < 1){
	        echo json_encode(array('returncode'=>'-1','message'=>'','servetime'=>'','data'=>array()));
	        exit();
	    }
	    
	    $servetime = $this->getParam('servetime');
	    $data = array();
	    
	    $lasttime = $this->ServeTable->getRowWithFields('createdate','','createdate desc');
	    if($lasttime['createdate'] > $servetime){
	        $servetime = $lasttime['createdate'];
	        
	        $redata = $this->ServeTable->listAll('','id asc');
	        foreach ($redata as $v){
	            if($v['machine_type'] == ($machine_type + 1) || $v['machine_type'] == 0){
	                $data[] = array(
	                    'gridcolor'=>$v['gridcolor'],
	                    'gridtag'=>$v['gridtag'],
	                    'gridtitle'=>$v['gridtitle'],
	                    'gridicon'=>$v['gridicon'],
	                    'gridurl'=>$v['gridurl']
	                );
	            }
	        }
	    }
	    
	    echo json_encode(array('returncode'=>'0','message'=>'成功','servetime'=>$servetime,'data'=>$data));
	    exit();
	}
	
	protected function out()
	{
		$this->layout->nav		= 'index';
		parent::out();
	}
}

Config::extend('ServeController', 'Controller');
